/**
 * 
 */
package inz.model;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.regex.Pattern;

import com.google.common.collect.Ordering;

/**
 *
 */
public class Model
{

	public void processFile(String fileName)
	{
		TreeMap<String, Integer> wordsMapByKey = new TreeMap<String, Integer>();
		NavigableMap<String, Integer> wordsMapByValue = new ValueComparableMap<String, Integer>(Ordering.natural());
		Scanner fileScanner;
		// String fileRegex = "[^\\p{L}]+";
		String fileRegex = "[\\W]+";
		// String fileRegex = "[^\\p{L}]*\\p{L}";
		int minSup = 1;
		try
		{
			fileScanner = new Scanner(new File(fileName));
			fileScanner.useDelimiter(Pattern.compile(fileRegex));
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
			return;
		}
		String word = "";
		while (fileScanner.hasNext())
		{
			word = fileScanner.next().toLowerCase();
			Integer wordCount = wordsMapByKey.get(word);
			if (wordCount != null)
			{
				wordsMapByKey.put(word, ++wordCount);
			}
			else
			{
				wordsMapByKey.put(word, 1);
			}
		}

		wordsMapByValue.putAll(wordsMapByKey);
		System.out.println("Map size:" + wordsMapByValue.size());

		for (Map.Entry<String, Integer> s : wordsMapByKey.entrySet())
		{
			if (s.getValue() >= minSup)
			{
				System.out.println(s);
			}
		}
		System.out.println("------------------------");
		for (Map.Entry<String, Integer> s : wordsMapByValue.descendingMap().entrySet())
		{
			if (s.getValue() >= minSup)
			{
				System.out.println(s);
			}
		}
	}
}
